class ChatMessage {
  final String id;
  final String senderId;
  final String receiverId;
  final String content;
  final MessageType type;
  final DateTime timestamp;
  final bool isRead;
  final String? attachmentUrl;
  final String? attachmentType;

  ChatMessage({
    required this.id,
    required this.senderId,
    required this.receiverId,
    required this.content,
    required this.type,
    required this.timestamp,
    this.isRead = false,
    this.attachmentUrl,
    this.attachmentType,
  });

  factory ChatMessage.fromJson(Map<String, dynamic> json) {
    return ChatMessage(
      id: json['id'],
      senderId: json['sender_id'],
      receiverId: json['receiver_id'],
      content: json['content'],
      type: MessageType.values.firstWhere(
        (e) => e.toString() == 'MessageType.${json['type']}',
        orElse: () => MessageType.text,
      ),
      timestamp: DateTime.parse(json['timestamp']),
      isRead: json['is_read'] ?? false,
      attachmentUrl: json['attachment_url'],
      attachmentType: json['attachment_type'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'sender_id': senderId,
      'receiver_id': receiverId,
      'content': content,
      'type': type.toString().split('.').last,
      'timestamp': timestamp.toIso8601String(),
      'is_read': isRead,
      'attachment_url': attachmentUrl,
      'attachment_type': attachmentType,
    };
  }
}

enum MessageType {
  text,
  image,
  file,
  voice,
  video,
}

class ChatSession {
  final String id;
  final String userId;
  final String otherUserId;
  final String otherUsername;
  final String otherUserAvatar;
  final String lastMessage;
  final DateTime lastMessageTime;
  final int unreadCount;
  final bool isOnline;
  final String? categoryId; // 关注分类ID

  ChatSession({
    required this.id,
    required this.userId,
    required this.otherUserId,
    required this.otherUsername,
    required this.otherUserAvatar,
    required this.lastMessage,
    required this.lastMessageTime,
    this.unreadCount = 0,
    this.isOnline = false,
    this.categoryId,
  });

  factory ChatSession.fromJson(Map<String, dynamic> json) {
    return ChatSession(
      id: json['id'],
      userId: json['user_id'],
      otherUserId: json['other_user_id'],
      otherUsername: json['other_username'],
      otherUserAvatar: json['other_user_avatar'],
      lastMessage: json['last_message'],
      lastMessageTime: DateTime.parse(json['last_message_time']),
      unreadCount: json['unread_count'] ?? 0,
      isOnline: json['is_online'] ?? false,
      categoryId: json['category_id'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'other_user_id': otherUserId,
      'other_username': otherUsername,
      'other_user_avatar': otherUserAvatar,
      'last_message': lastMessage,
      'last_message_time': lastMessageTime.toIso8601String(),
      'unread_count': unreadCount,
      'is_online': isOnline,
      'category_id': categoryId,
    };
  }
}

class ConsultationRecord {
  final String id;
  final String userId;
  final String consultantId;
  final String consultantName;
  final String consultantAvatar;
  final String topic;
  final String description;
  final ConsultationStatus status;
  final DateTime startTime;
  final DateTime? endTime;
  final int duration; // 分钟
  final double amount; // 金额
  final String? notes;
  final double? rating;
  final String? review;

  ConsultationRecord({
    required this.id,
    required this.userId,
    required this.consultantId,
    required this.consultantName,
    required this.consultantAvatar,
    required this.topic,
    required this.description,
    required this.status,
    required this.startTime,
    this.endTime,
    this.duration = 0,
    this.amount = 0.0,
    this.notes,
    this.rating,
    this.review,
  });

  factory ConsultationRecord.fromJson(Map<String, dynamic> json) {
    return ConsultationRecord(
      id: json['id'],
      userId: json['user_id'],
      consultantId: json['consultant_id'],
      consultantName: json['consultant_name'],
      consultantAvatar: json['consultant_avatar'],
      topic: json['topic'],
      description: json['description'],
      status: ConsultationStatus.values.firstWhere(
        (e) => e.toString() == 'ConsultationStatus.${json['status']}',
        orElse: () => ConsultationStatus.pending,
      ),
      startTime: DateTime.parse(json['start_time']),
      endTime:
          json['end_time'] != null ? DateTime.parse(json['end_time']) : null,
      duration: json['duration'] ?? 0,
      amount: (json['amount'] as num?)?.toDouble() ?? 0.0,
      notes: json['notes'],
      rating: (json['rating'] as num?)?.toDouble(),
      review: json['review'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'consultant_id': consultantId,
      'consultant_name': consultantName,
      'consultant_avatar': consultantAvatar,
      'topic': topic,
      'description': description,
      'status': status.toString().split('.').last,
      'start_time': startTime.toIso8601String(),
      'end_time': endTime?.toIso8601String(),
      'duration': duration,
      'amount': amount,
      'notes': notes,
      'rating': rating,
      'review': review,
    };
  }
}

enum ConsultationStatus {
  pending, // 待开始
  ongoing, // 进行中
  completed, // 已完成
  cancelled, // 已取消
}
